from django.shortcuts import render

# Create your views here.

from django.views.generic import View
from django.http import HttpResponse, JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import AllowAny
from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView, UpdateAPIView, DestroyAPIView, CreateAPIView, ListCreateAPIView, RetrieveUpdateAPIView, RetrieveDestroyAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, CreateModelMixin


from .models import SysPost
from .serializers import SysPostSerializer

import json

#django view 视图
class SysPostListView(View):
    '''
    岗位列表视图
    '''
    def get(self, request):
        sys_posts = SysPost.objects.all()
        json_data = []
        for sys_post in sys_posts:
            json_data.append({'post_id': sys_post.post_id,
                              'post_code': sys_post.post_code,
                              'post_name': sys_post.post_name,
                              'post_sort': sys_post.post_sort,
                              'status': sys_post.status})
        return JsonResponse(json_data, safe=False)

    def post(self, request):
        request_data = json.loads(request.body.decode('utf-8'))
        post_code = request_data.get('post_code')
        post_name = request_data.get('post_name')
        post_sort = request_data.get('post_sort')
        status = request_data.get('status')
        sys_post = SysPost(post_code=post_code, post_name=post_name, post_sort=post_sort, status=status)
        sys_post.save()
        return JsonResponse({'post_id': sys_post.post_id,
                             'post_code': sys_post.post_code,
                             'post_name': sys_post.post_name,
                             'post_sort': sys_post.post_sort,
                            'status': sys_post.status}, safe=False)



class SysPostDetailView(View):
    ''' 岗位详情视图'''
    def get(self, request, pk):
        try:
            sys_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return HttpResponse(status=404)
        json_data = {'post_id': sys_post.post_id,
                     'post_code': sys_post.post_code,
                     'post_name': sys_post.post_name,
                     'post_sort': sys_post.post_sort,
                    'status': sys_post.status}
        return JsonResponse(json_data, safe=False)

    def put(self, request, pk):
        try:
            sys_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return HttpResponse(status=404)
        request_data = json.loads(request.body.decode('utf-8'))
        post_code = request_data.get('post_code')
        post_name = request_data.get('post_name')
        post_sort = request_data.get('post_sort')
        status = request_data.get('status')
        sys_post.post_code = post_code
        sys_post.post_name = post_name
        sys_post.post_sort = post_sort
        sys_post.status = status
        sys_post.save()
        return JsonResponse({'post_id': sys_post.post_id,
                             'post_code': sys_post.post_code,
                             'post_name': sys_post.post_name,
                             'post_sort': sys_post.post_sort,
                            'status': sys_post.status}, safe=False)

    def delete(self, request, pk):
        try:
            sys_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return HttpResponse(status=404)
        sys_post.delete()
        return HttpResponse(status=204)



#APIview 视图

class SysPostListAPIView(APIView):
    permission_classes = [AllowAny]  # 允许任何用户访问登录接口
    '''
    岗位列表API视图
    '''
    def get(self, request):
        system_posts = SysPost.objects.all()
        serializer = SysPostSerializer(system_posts, many=True)
        return Response(serializer.data)

    def post(self, request):
        serializer = SysPostSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

class SysPostDetailAPIView(APIView):
    '''岗位详情API视图'''

    def get(self, request, pk):
        try:
            system_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = SysPostSerializer(system_post)
        return Response(serializer.data)

    def put(self, request, pk):
        try:
            system_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = SysPostSerializer(system_post, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def delete(self, request, pk):
        try:
            system_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        system_post.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class SysPostListGenericAPIView(GenericAPIView):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer
    def get(self, request):
        system_posts = self.get_queryset()
        serializer = self.get_serializer(system_posts, many=True)
        return Response(serializer.data)
    def post(self, request):
        serializer=self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

class SysPostDetailGenericAPIView(GenericAPIView):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer
    def get(self, request, pk):
        system_post = self.get_object()
        serializer =self.get_serializer(system_post)
        return Response(serializer.data)
    def put(self, request, pk):
        system_post = self.get_object()
        seriazer = self.get_serializer(system_post, data=request.data)
        seriazer.is_valid(raise_exception=True)
        seriazer.save()
        return Response(seriazer.data)
    def delete(self, request, pk):
        system_post = self.get_object()
        system_post.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class SysPostListGenericMixinAPIView(ListModelMixin,CreateModelMixin,GenericAPIView):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer
    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class SysPostDetailGenericMixinAPIView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer
    def get(self, request, pk):
        return self.retrieve(request, pk)

    def put(self, request, pk):
        return self.update(request, pk)

    def delete(self, request, pk):
        return self.destroy(request, pk)



class SysPostListGenericsAPIView(ListCreateAPIView):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer



class SysPostDetailGenericsAPIView(RetrieveUpdateDestroyAPIView):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer


from rest_framework import viewsets

from rest_framework.viewsets import GenericViewSet, ModelViewSet

class SysPostViewSet(viewsets.ViewSet):
    def list(self, request):
        system_posts = SysPost.objects.all()
        serializer = SysPostSerializer(system_posts, many=True)
        return Response(serializer.data)

    def create(self, request):
        serializer = SysPostSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

    def retrieve(self, request, pk=None):
        try:
            system_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = SysPostSerializer(system_post)
        return Response(serializer.data)

    def update(self, request, pk=None):
        try:
            system_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = SysPostSerializer(system_post, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def destroy(self, request, pk=None):
        try:
            system_post = SysPost.objects.get(post_id=pk)
        except SysPost.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        system_post.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


from rest_framework.viewsets import GenericViewSet, ModelViewSet

class SysPostGenericViewSet(GenericViewSet):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer

    def list(self, request):
        system_posts = self.get_queryset()
        serializer = self.get_serializer(system_posts, many=True)
        return Response(serializer.data)

    def create(self, request):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

    def retrieve(self, request, pk=None):
        system_post = self.get_object()
        serializer = self.get_serializer(system_post)
        return Response(serializer.data)

    def update(self, request, pk=None):
        system_post = self.get_object()
        serializer = self.get_serializer(system_post, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def destroy(self, request, pk=None):
        system_post = self.get_object()
        system_post.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


from rest_framework.viewsets import GenericViewSet, ModelViewSet
from rest_framework import mixins

class SysPostGenericMixinsViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, GenericViewSet):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer

from rest_framework.decorators import action
class SysPostModelViewSet(ModelViewSet):
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer

    @action(methods=['get'], detail=False)
    def lasest_post(self, request):
        system_post = SysPost.objects.latest('post_id')
        serializer = SysPostSerializer(system_post)
        return Response(serializer.data)
    @action(methods=['put'], detail=True)
    def update_post_name(self, request,pk):
        system_post = self.get_object()
        serializer = SysPostSerializer(system_post, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)